<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Static
 *
 * @author Andriy
 */
class Core_Static
{

    protected $_css_base_path = "/-/css/";

    protected $_js_base_path = "/-/js/";

    protected $_design_base_path = "/-/design/";

    protected $_upload_base_path = "/-/upload/";

    protected $_css = array();
    
    protected $_js = array();

    protected $_toJs = array();

    public function setConfigs( $configs )
    {
        if( isset( $configs['css'] ) )
            $this->_css_base_path = $configs['css'];

        if( isset( $configs['js'] ) )
            $this->_css_base_path = $configs['js'];

        if( isset( $configs['design'] ) )
            $this->_css_base_path = $configs['design'];
        
        if( isset( $configs['upload'] ) )
            $this->_css_base_path = $configs['upload'];
    }

    protected function _isHttp( $path )
    {
        if( strpos( $path, 'http' ) !== false )
        {
            return true;
        }
        return false;
    }

    public function css( $path, $is_first = false )
    {
        $path = trim( $path, '/' );
        $path = trim( $path, '\\' );
        if( !$this->_isHttp( $path ) )
        {
            $path = $this->_css_base_path . $path;
        }

        $link = '<link href="' . $path . '" rel="stylesheet" type="text/css" />';

        if( $is_first )
        {
            array_unshift( $this->_css, $link );
        }
        else
        {
            $this->_css[] = $link;
        }
    }

    public function js( $path, $is_first = false )
    {
        $path = trim( $path, '/' );
        $path = trim( $path, '\\' );
        if( !$this->_isHttp( $path ) )
        {
            $path = $this->_js_base_path . $path;
        }

        $link = '<script language="javascript" type="text/javascript" src="' . $path . '"></script>';

        if( $is_first )
        {
            array_unshift( $this->_js, $link );
        }
        else
        {
            $this->_js[] = $link;
        }
    }

    public function design( $path )
    {
        $path = trim( $path, '/' );
        $path = trim( $path, '\\' );
        if( !$this->_isHttp( $path ) )
        {
            $path = $this->_design_base_path . $path;
        }

        return $path;
    }

    public function toJs( $key, $value )
    {
        $this->_toJs[$key] = $value;
    }

    public function renderCss()
    {
        return implode( "\r\n", $this->_css );
    }

    public function renderJs()
    {
        return implode( "\r\n", $this->_js );
    }

    public function renderToJs()
    {
        $script = '
        <script language="javascript" type="text/javascript">
            function Js(){};
            Js.data = '. json_encode( $this->_toJs ) .'
            Js._ = function( $key )
            {
                return Js.data[$key];
            }
            Js.get = function( $key )
            {
                return Js.data[$key];
            }
        </script>';

        return $script;
    }

    public function render()
    {
        $render = array();
        $render[] = $this->renderCss();
        $render[] = $this->renderJs();
        $render[] = $this->renderToJs();

        return implode( "\r\n", $render );
    }
    

}
?>
